package com.company.psm.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.company.psm.entity.Gradient;
import com.company.psm.entity.User;
import com.company.psm.service.GradientManagement;
import com.company.psm.service.MedicineManagement;
import com.company.psm.service.UserManagement;

@Controller
@RequestMapping("/gradient")
public class GradientController {
	@Autowired
	private GradientManagement gradientManagement;
	@Autowired
	private MedicineManagement medicineManagement;
	@Autowired
	private UserManagement userManagement;
	@RequestMapping(method = RequestMethod.GET, value = "list")
	public ModelAndView getAllGradients(@RequestParam(value="listAll", required=false) String listAll) {
		List<Gradient> gradients = new ArrayList<Gradient>();
		Long storeId = getCurrentUser().getStore().getId();
		if (listAll!=null && listAll.equals("listAllData")){
			gradients = gradientManagement.getListOfGradientsByStoreId(storeId);
		}
		return new ModelAndView("/gradient/list", "gradients",
				gradients);
	}

	@RequestMapping(method = RequestMethod.GET, value = "create")
	public ModelAndView createNewGradient(
			@RequestParam(value = "gradientName", required = false) String gradientName) {
		if (gradientName != null) {
			System.out.println(gradientName);
			long store_id = this.getCurrentUser().getStore().getId();
			gradientManagement.createNewGradient(gradientName, store_id);
			return new ModelAndView("/gradient/list", "gradients",
					gradientManagement.getListOfGradientsByStoreId(store_id));
		} else {
			return new ModelAndView("/gradient/create", "gradients",
					gradientManagement.getListOfGradientsByStoreId(this.getCurrentUser().getStore().getId()));
		}
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "update")
	public ModelAndView update(
			@RequestParam(value = "gradientId", required = false) String gradientId,
			@RequestParam(value = "updateName", required = false) String gradientName,
			@RequestParam(value = "storeId", required = false) String storeId){
			gradientManagement.updateGradient(gradientId, gradientName, this.getCurrentUser().getStore().getId());
			return new ModelAndView("/gradient/list", "gradients",
					gradientManagement.getListOfGradientsByStoreId(this.getCurrentUser().getStore().getId()));
	}
	@RequestMapping(method = RequestMethod.GET, value = "adminCpanel")
	public ModelAndView adminCPanel() {
		return new ModelAndView("/gradient/adminCpanel", "gradients",
				null);
	}
    public User getCurrentUser(){
    	Authentication authentication = SecurityContextHolder.getContext().
      		  getAuthentication();
      		  String name = authentication.getName();
      	return userManagement.getUserByAccount(name);
    }
}
